package com.docker.apps.afterservice.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dcbfhd.utilcode.utils.ToastUtils;
import com.docker.apps.R;
import com.docker.apps.afterservice.vm.AfterServiceViewModel;
import com.docker.apps.databinding.ProAfterServiceDetailActivityBinding;
import com.docker.apps.order.vo.AfterServiceVo;
import com.docker.apps.order.vo.OrderVoV2;
import com.docker.apps.order.vo.ServiceDetailVo;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.ui.base.NitCommonActivity;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.common.common.utils.rxbus.RxBus;
import com.docker.common.common.utils.rxbus.RxEvent;
import com.docker.common.common.vo.UserInfoVo;

import java.util.HashMap;

/**
 * kxf -> 2020-02-18
 **/
@Route(path = AppRouter.ORDER_AFTER_DETAIL)
public class AfterServiceDetailActivity extends NitCommonActivity<AfterServiceViewModel, ProAfterServiceDetailActivityBinding> {

    @Override
    public void initView() {
        mToolbar.setTitle("退/换单详情");
        String refundsNo = getIntent().getStringExtra("refundsNo");
        UserInfoVo userInfoVo = CacheUtils.getUser();
        HashMap<String, String> params = new HashMap<>();
        params.put("memberid", userInfoVo.uid);
        params.put("uuid", userInfoVo.uuid);
        params.put("refundsNo", refundsNo);
        mViewModel.getServiceDetail(params);
        mBinding.tvLookQxsq.setOnClickListener(v -> {
            v.setVisibility(View.GONE);
            params.put("orderid", detailVo.getId());
            params.put("goodsid", detailVo.getGoodsid());
            params.put("refundsNo", refundsNo);
            mViewModel.cancelService(params);

        });
        mBinding.tvLookXgsq.setOnClickListener(v -> {
//            Timber.e(JSON.toJSONString(goodsInfo));
//            OrderVoV2.GoodsInfo goodsInfo = new OrderVoV2.GoodsInfo();
//            goodsInfo.name = detailVo.getGoodsName();
//            goodsInfo.num = detailVo.getGoodsNum();
//            goodsInfo.img = detailVo.getGoodsImg();
//            goodsInfo.price = detailVo.getGoodsPrice();

            AfterServiceVo.AfterServiceGood afterServiceGood = new AfterServiceVo.AfterServiceGood();
            afterServiceGood.goodsName = detailVo.getGoodsName();
            afterServiceGood.goodsNum = detailVo.getGoodsNum();
            afterServiceGood.goodsImg = detailVo.getGoodsImg();
            afterServiceGood.price = detailVo.getGoodsPrice();
            afterServiceGood.orderid = detailVo.getId();
            afterServiceGood.returnId = detailVo.returnId;
            afterServiceGood.goodsid = detailVo.getGoodsid();

            ARouter.getInstance().build(AppRouter.ORDER_AFTER_CHOOSE)
                    .withString("orderid", afterServiceGood.orderid)
                    .withSerializable("goodsInfo", afterServiceGood)
                    .withString("returnId", afterServiceGood.returnId)
                    .navigation();
            finish();

//            ARouter.getInstance().build(AppRouter.ORDER_AFTER_CHOOSE)
//                    .withString("orderid", detailVo.getId())
//                    .withSerializable("goodsInfo", goodsInfo)
//                    .navigation();
        });

        mBinding.orderDeCopy.setOnClickListener(v -> {
            if (mBinding.getItem() != null) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                assert cm != null;
                cm.setPrimaryClip(ClipData.newPlainText("copy", refundsNo));
                ToastUtils.showShort("复制成功");
            }
        });
        mBinding.tvLookThwl.setOnClickListener(v -> {
            if (mBinding.getItem() != null) {
                ARouter.getInstance().build(AppRouter.ORDER_AFTER_EDIT_WULIU)
                        .withString("refundsNo", refundsNo)
                        .navigation();
//                this.finish();
            }
        });


    }

    ServiceDetailVo detailVo;

    @Override
    public void initObserver() {
        mViewModel.mServerLiveData.observe(this, o -> {
            if (o instanceof ServiceDetailVo && o != null) {
                detailVo = (ServiceDetailVo) o;
                mBinding.setItem(detailVo);
                notiDetailStatus();
            } else {
                RxBus.getDefault().post(new RxEvent<>("updateAfterService", 1));
                ToastUtils.showShort("取消成功");
                finish();
            }
        });
    }

    @Override
    public void initRouter() {
        ARouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pro_after_service_detail_activity;
    }

    @Override
    public AfterServiceViewModel getmViewModel() {
        return ViewModelProviders.of(this, factory).get(AfterServiceViewModel.class);
    }

    private void notiDetailStatus() {
//        status=0 待审核
//        商家审核同意 status=1
//        商家拒绝 status=3
//        买家邮寄物流 status=4 商家等待接受货物
//        商家确认收货 并退款 status=2 申请完成
//        取消申请 status=10
        if (detailVo == null || TextUtils.isEmpty(detailVo.getStatus())) {
            ToastUtils.showLong("订单不存在");
            finish();
            return;
        }
        String status = "";
        switch (detailVo.getStatus()) {
            case "0":
                status = "待审核";
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterT4.setVisibility(View.VISIBLE);
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterT4.setVisibility(View.VISIBLE);
                mBinding.afterI2.setVisibility(View.VISIBLE);
                mBinding.afterI3.setVisibility(View.VISIBLE);
                mBinding.afterT5.setVisibility(View.VISIBLE);
                mBinding.tvLookQxsq.setVisibility(View.VISIBLE);
                mBinding.tvLookXgsq.setVisibility(View.VISIBLE);
                break;

            case "1":
                status = "卖家同意";
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterT4.setVisibility(View.VISIBLE);
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterI4.setVisibility(View.VISIBLE);
                mBinding.afterI5.setVisibility(View.VISIBLE);
                mBinding.afterI2.setVisibility(View.VISIBLE);
                mBinding.afterI3.setVisibility(View.VISIBLE);
                mBinding.afterT5.setVisibility(View.VISIBLE);
                mBinding.afterI5.setImageResource(R.mipmap.jindu_bai);
                mBinding.afterT6.setText("卖家同意");
                mBinding.tvLookThwl.setVisibility(View.VISIBLE);
                mBinding.afterT5.setVisibility(View.VISIBLE);

                break;
            case "2":
                status = "已完成";
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterI2.setVisibility(View.VISIBLE);
                mBinding.afterI3.setVisibility(View.VISIBLE);
                mBinding.afterI4.setVisibility(View.VISIBLE);
                mBinding.afterI7.setVisibility(View.VISIBLE);
                mBinding.afterI6.setVisibility(View.VISIBLE);
                mBinding.afterI5.setImageResource(R.mipmap.jindu_bai);
                mBinding.afterT6.setVisibility(View.VISIBLE);
                mBinding.afterT4.setVisibility(View.VISIBLE);
                mBinding.afterT7.setVisibility(View.VISIBLE);
                mBinding.afterT6.setText("卖家同意");
                mBinding.afterChangeLiner.setVisibility(View.GONE);
                mBinding.afterI5.setVisibility(View.VISIBLE);
                mBinding.afterT5.setVisibility(View.VISIBLE);
                break;
            case "3":
                status = "商家拒绝";
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterI2.setVisibility(View.VISIBLE);
                mBinding.afterI3.setVisibility(View.VISIBLE);
                mBinding.afterI4.setVisibility(View.VISIBLE);
                mBinding.afterI5.setVisibility(View.VISIBLE);
                mBinding.afterI5.setImageResource(R.mipmap.after_jujue);
                mBinding.afterT6.setVisibility(View.VISIBLE);
                mBinding.afterT6.setText(status);
                mBinding.afterChangeLiner.setVisibility(View.GONE);
                mBinding.afterT5.setVisibility(View.VISIBLE);
                break;
            case "4":
            case "5":
                status = "卖家同意";
                mBinding.afterI1.setVisibility(View.VISIBLE);
                mBinding.afterI2.setVisibility(View.VISIBLE);
                mBinding.afterI3.setVisibility(View.VISIBLE);
                mBinding.afterI4.setVisibility(View.VISIBLE);
                mBinding.afterT4.setVisibility(View.VISIBLE);
                mBinding.afterI5.setVisibility(View.VISIBLE);
                mBinding.afterI5.setImageResource(R.mipmap.jindu_bai);
                mBinding.afterT6.setVisibility(View.VISIBLE);
                mBinding.afterT6.setText(status);
                mBinding.afterChangeLiner.setVisibility(View.GONE);
                mBinding.afterT5.setVisibility(View.VISIBLE);
                break;
            case "10":
                status = "已取消";
                break;
        }
        mBinding.detailTxtStatus.setText(status);
    }
}